<template>
  <!-- 添加或修改运货作业单对话框 -->
  <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body :close-on-click-modal="false"
    @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
      size="small">
      <el-form-item label="提货场所" prop="startFieldCode">
        <el-select v-model="form.startFieldCode" placeholder="请选择提货场所" >
          <el-option v-for="item in selectBaseFieldInfoList" :key="item.value" :value="item.value"
                     :label="item.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="核扣类型" prop="deductType">
        <el-select v-model="form.deductType" placeholder="请选择核扣类型" @change="deductTypeChange">
          <el-option v-for="item in dict.type.bulk_cargo_deduct_type" :key="item.value" :value="item.value"
                     :label="item.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="散杂货类型" prop="bulkCargoType">
        <el-select v-model="form.bulkCargoType" placeholder="请选择散杂货类型">
          <el-option v-for="item in dict.type.bill_je_gate_type" :key="item.value" :value="item.value"
                     :label="item.label"></el-option>
        </el-select>
      </el-form-item>
       <el-form-item label="报关企业" prop="declCopCode">
         <el-select v-model.trim="form.declCopCode"  filterable clearable placeholder="请选择报关企业">
           <el-option v-for="dict in selectBgEntInfoList" :key="dict.value" :label="dict.label"
             :value="dict.value"></el-option>
         </el-select>
       </el-form-item>
       <el-form-item label="货主企业" prop="cargoOwnCode">
         <el-select v-model.trim="form.cargoOwnCode" filterable clearable placeholder="请选择货主企业">
           <el-option v-for="dict in selectHzEntInfoList" :key="dict.value" :label="dict.label"
             :value="dict.value"></el-option>
         </el-select>
       </el-form-item>
    </el-form>
    <div class="ui-basic-content">
      <el-tabs v-model="activeName" type="card" >
        <el-tab-pane label="商品列表" name="tabGoods">
          <!-- 货物信息 -->
          <tab-goods ref="addTabGoods" :dialogType="dialogType" :deductType="deductType" :editGoodsData="editGoodsList"></tab-goods>
        </el-tab-pane>
        <el-tab-pane label="车辆列表" name="tabVehicle">
          <!-- 运输工具信息 -->
          <tab-vehicle ref="addTabVehicle" :dialogType="dialogType" :editVehicleData="editVehicleList" :editContaData="editContaList"></tab-vehicle>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getEditGTakecargoBills,
    addGTakecargoBills,
    updateGTakecargoBills
  } from "@/api/billsdecl/gTakecargoBills";
  import tabGoods from "./tabGoods.vue";
  import tabVehicle from "./tabVehicle.vue";
   import {findBaseFieldInfo,getBaseFieldInfo} from "@/api/system/baseFieldInfo";
   import {findBusinessTypeList} from "@/api/billsdecl/billBusinessType";
   import {findListEnterpriseInfo} from "@/api/system/enterpriseInfo";
  export default {
    name: "GTakecargoBills-addEdit",
    components: {
      tabGoods,
      tabVehicle
    },
    props: {
      dialogType: {
        type: String,
        default: "add",
      },
      rowData: {
        type: Object,
        default: () => {},
      },
      dict: {}
    },
    data() {
      return {
        // 弹出层
        dialogVisible: false,
        // 标题
        title: "新增",
        activeName: 'tabGoods',
        //编辑ID
        editPlatSeqNo:null,
        //核扣类型
        deductType:'1',//默认申报核扣
        //单证分类，7-提货类型
        billCategory:'7',
        userCustomsCode: this.$store.state.user.userLoginInfo.customsCode,
        userFieldCode: this.$store.state.user.userLoginInfo.fieldCode,
        //下来场所列表
        selectBaseFieldInfoList: [],
        //下来报关企业列表
        selectBgEntInfoList: [],
        //下来货主企业列表
        selectHzEntInfoList: [],
        // 表单参数
        form: {
          platSeqNo: null,
          fieldCode: null,
          customsCode: null,
          startFieldCode: null,
          declCopCode: null,
          cargoOwnCode: null,
          iEFlag: 'I',
          businessType: '11',
          deductType: null,
          bulkCargoType: null,
          remark: null

        },
        // 运货作业单表格数据
        gTakecargoBillsList: [],
        //编辑反写货物列表
        editGoodsList:[],
        //编辑反写车辆列表
        editVehicleList:[],
        //编辑反写集装箱列表
        editContaList:[],
        // 表单校验
        rules: {
          startFieldCode: [{
            required: true,
            message: "提货场所不能为空",
            trigger: "blur"
          }],
          deductType: [{
            required: true,
            message: "提货核扣类型不能为空",
            trigger: "change"
          }],
          bulkCargoType: [{
            required: true,
            message: "散杂货标志不能为空",
            trigger: "change"
          }],
        }
      };
    },
    methods: {
      open(plateSeqNo) {
        this.initData();
        this.$nextTick(() => {
          if (this.dialogType == "add") {
            this.title = "新增";
             this.dialogVisible = true;
          } else if (this.dialogType == "edit") {
            this.title = "编辑";
            this.editPlatSeqNo=plateSeqNo;
            this.loadDetail();
          } else if (this.dialogType == "detail") {
            this.title = "详情";
            this.loadDetail();
          }
          this.getLoadBaseFieldInfoList();
          this.getLoadUserField();
          this.getLoadEntInfoList();
        });
      },
      close() {
        this.$refs["form"].resetFields();
        this.dialogVisible = false;
      },
      //初始化数据
      initData(){
        // 运货作业单表格数据
        this.gTakecargoBillsList=[];
        //编辑反写货物列表
        this.editGoodsList=[];
        //编辑反写车辆列表
        this.editVehicleList=[];
        //编辑反写集装箱列表
        this.editContaList=[];
      },
      //加载用户场所信息
      getLoadUserField(){
        if(null==this.userFieldCode){
          return;
        }
        getBaseFieldInfo(this.userFieldCode).then((res) => {
          if (res.code == 200) {
            //this.form = res.data;
              this.form.workFieldCode=this.userFieldCode;
            if(res.data.fieldType=='3'){//后置场所
               this.rules.gateFieldCode[0].required=true;
               this.gateFieldItemShow=true;
            }
          }
        });
      },
      //获取场所下拉列表
      getLoadBaseFieldInfoList() {
       findBaseFieldInfo({customsCode:this.userCustomsCode,takeCargoField:'1'}).then(response => {
          this.selectBaseFieldInfoList = response.data;
          if(this.selectBaseFieldInfoList!=null&&this.selectBaseFieldInfoList.length==1){
            this.form.workFieldCode=this.selectBaseFieldInfoList[0].value;
          }
          if(this.selectBaseFieldInfoList.length>0){
             this.selectGateBaseFieldInfoList=this.selectBaseFieldInfoList.filter(n=>"2"==n.extVal1);
          }

       })
      },//获取企业下拉列表
      getLoadEntInfoList() {
       findListEnterpriseInfo({}).then(response => {
         let entList=response.data;
         if(entList!=null&&entList.length>0){
           this.selectBgEntInfoList=entList.filter(n=>"1"==n.extVal1);
           this.selectHzEntInfoList=entList.filter(n=>"2"==n.extVal1);
         }
       })
      },
      deductTypeChange(val) {
       this.deductType=val;
      },

      // 获取详情
      loadDetail() {
        getEditGTakecargoBills(this.editPlatSeqNo).then((res) => {
          if (res.code == 200) {
            this.form = res.data;
            //反写货物列表
            this.editGoodsList=res.data.goodsList;
            //反写车辆列表
            this.editVehicleList=res.data.vehicleList;
            //反写集装箱列表
            this.editContaList=res.data.contaList;
             this.dialogVisible = true;
          }
        });
      },

      // 提交表单
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let params = {
              ...this.form,
            };
            let goodsList=this.$refs.addTabGoods.tableData;
            let vehicleList=this.$refs.addTabVehicle.submitVehicleList;
            let contaList=this.$refs.addTabVehicle.vahicleContaList;
            params.goodsList=goodsList;
            params.vehicleList=vehicleList;
            params.contaList=contaList;
            if (this.dialogType == "add") {
              addGTakecargoBills(params).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            } else {
              params.id = this.rowData.id;
              updateGTakecargoBills(params).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            }
          }
        });
      },
    },
  };
</script>

<style lang="scss" scoped></style>
